package com.free4k.hss.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.free4k.hss.config.MyResult;
import com.free4k.hss.entity.PoemArticle;
import com.free4k.hss.service.PoemArticleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

//测试接口   mp+mybatis查询
//包含mp常用方法
@RestController
@RequestMapping("poemArticle")
public class PoemArticleController{

    @Resource
    private PoemArticleService poemArticleService;

    //  127.0.0.1:8081/poemArticle/list?page=1&pageSize=10
    @GetMapping("list")
    public Page<PoemArticle> list(int page, int pageSize){
        Page<PoemArticle> mpPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<PoemArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PoemArticle::getId);
        poemArticleService.page(mpPage, wrapper);
        return mpPage;
    }

    //  127.0.0.1:8081/poemArticle/likeName?authorName=李白
    @GetMapping("likeName")
    public MyResult likeName(String authorName){
        List<PoemArticle> list =poemArticleService.selectLikeName(authorName);
        return MyResult.ok("success",list);
    }

/*

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    queryWrapper.eq("name", "张三");          // 等于 =
    queryWrapper.ne("name", "李四");          // 不等于 <>
    queryWrapper.gt("age", 18);         // 大于 >
    queryWrapper.ge("age", 18);         // 大于等于 >=
    queryWrapper.lt("age", 65);         // 小于 <
    queryWrapper.le("age", 65);         // 小于等于 <=
    queryWrapper.between("age", 18, 30);        // BETWEEN 值1 AND 值2
    queryWrapper.like("name", "张");            // LIKE '%值%'
    queryWrapper.likeLeft("name", "张");         // 左 LIKE '值%'
    queryWrapper.likeRight("name", "三");        // 右 LIKE '%值'
    queryWrapper.in("id", 1, 2, 3);            // IN (v0, v1, ...)
    queryWrapper.notIn("id", 4, 5, 6);         // NOT IN (v0, v1, ...)
    queryWrapper.isNull("email");           // IS NULL
    queryWrapper.isNotNull("phone");        // IS NOT NULL
    queryWrapper.groupBy("department");     // 分组：GROUP BY 字段, ...
    queryWrapper.orderByAsc("age");             // 排序：ORDER BY 字段, ... ASC/DESC
    queryWrapper.orderByDesc("create_time");    // 排序：ORDER BY 字段, ... ASC/DESC
    queryWrapper.having("count(*) > 10");        // HAVING (sql语句)


    // 条件构造器查询
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "张三").gt("age", 20);
    List<User> users = userMapper.selectList(queryWrapper);

    // lambda查询
    LambdaQueryWrapper<User> lambdaQuery = new LambdaQueryWrapper<>();
    lambdaQuery.eq(User::getName, "张三")
               .gt(User::getAge, 20)
               .like(User::getEmail, "@example.com");
    List<User> users = userMapper.selectList(lambdaQuery);

---------------------------------------------------------------------------------------

// 根据ID查询
User user = userMapper.selectById(1L);

// 批量ID查询
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));

// 按条件查询单个实体
User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", "张三"));

// 按条件查询列表
List<User> users = userMapper.selectList(new QueryWrapper<User>().gt("age", 18));

// 按条件查询总记录数
Long count = userMapper.selectCount(new QueryWrapper<User>().eq("status", 1));

// 按条件查询是否存在
boolean exists = userMapper.exists(new QueryWrapper<User>().eq("email", "test@example.com"));

// 按ID查询返回Map
Map<String, Object> userMap = userMapper.selectMapById(1L);

// 按条件查询返回Map列表
List<Map<String, Object>> mapList = userMapper.selectMaps(new QueryWrapper<User>().select("id", "name"));

// 按条件查询返回Map（第一条记录）
Map<String, Object> firstMap = userMapper.selectMaps(new QueryWrapper<User>().last("LIMIT 1")).get(0);

// 普通分页查询
Page<User> page = new Page<>(1, 10);
Page<User> userPage = userMapper.selectPage(page, new QueryWrapper<User>().orderByDesc("create_time"));

// 返回Map的分页查询
Page<Map<String, Object>> mapPage = userMapper.selectMapsPage(page, new QueryWrapper<User>());

// Lambda条件查询
List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>()
        .eq(User::getName, "张三")
        .gt(User::getAge, 18));

// Lambda链式调用 (MyBatis-Plus 3.x+)
List<User> userList = userMapper.lambdaQuery()
        .eq(User::getStatus, 1)
        .like(User::getName, "张")
        .list();


*/

}

